#!/usr/bin/env python

# funcsigshs.py - This is a LSL2dfg.py output module for LSLForge FuncSigs.hs Haskell output.
#
# (C) Copyright 2013 Sei Lisa.
# Sei Lisa is the author's username in the Second Life online virtual world.
#
# This file is part of LSL2 Derived Files Generator.
#
#    LSL2 Derived Files Generator is free software: you can redistribute it
#    and/or modify it under the terms of the GNU Lesser General Public License
#    as published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    LSL2 Derived Files Generator is distributed in the hope that it will be
#    useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Lesser General Public License for more details.
#
#    You should have received a copy of the GNU Lesser General Public License
#    along with LSL2 Derived Files Generator. If not, see
#    <http://www.gnu.org/licenses/>.
#
# Second Life is a trademark of Linden Research, Inc.


import sys

def output(document, defaultdescs, databaseversion, infilename, outfilename, lang, tag):

  version = "0.0.20130817000"

  if lang not in defaultdescs:
    defaultdescs[lang] = defaultdescs["default"]

  hstype = {"integer":"LLInteger", "float":"LLFloat",
            "string":"LLString", "key":"LLKey",
            "rotation":"LLRot", "quaternion":"LLRot",
            "vector":"LLVector", "list":"LLList"}

  try:
    document.sort(key=lambda x: x["name"])
  except:
    document.sort(lambda x,y: cmp(x["name"],y["name"]))

  if infilename is not None:
    inf = open(infilename, "r")
  else:
    inf = sys.stdin

  try:
    inputlines = inf.readlines()

  finally:
    if infilename is not None:
      inf.close()

  if outfilename is not None:
    outf = open(outfilename, "w")
  else:
    outf = sys.stdout

  try:

    for line in inputlines:
      if not line.startswith("<<< %s KEYWORDS >>>" % tag):
        outf.write(line)
      else:
        outf.write("-- Generated by LSL2 Derived Files Generator. Database version: %s; output module version: %s\n"
          % (databaseversion, version))

        outf.write("funcSigs = [\n")

        newline = ""
        for element in document:
          if element["cat"] == "function" and ("status" not in element or element["status"] != "godmode"):
            if newline != "":
              outf.write(newline + ",\n")
            newline = '    ("' + element['name'] + '",'
            if "type" in element:
              newline = newline + hstype[element["type"]] + ",["
            else:
              newline = newline + "LLVoid,["
            if "params" in element:
              for param in element["params"]:
                newline = newline + hstype[param["type"]] + ","
              newline = newline[:-1]
            newline = newline + "])"

        if newline != "":
          outf.write(newline + "]\n")
        else:
          outf.write("    ]\n")

        outf.write("\nfuncDescriptions = [\n")
        newline = ""
        for element in document:
          if element["cat"] == "function" and ("status" not in element or element["status"] != "godmode"):
            if "desc" in element and lang in element["desc"]:
              desc = element["desc"][lang]["text"]
            else:
              desc = defaultdescs[lang]
            if newline != "":
              outf.write(newline + ",\n")
            newline = '    ("' + element['name'] + '",(['
            if "params" in element:
              for param in element["params"]:
                newline = newline + '"' + param["name"] + '",'
              newline = newline[:-1]
            newline = newline + '],"' + desc.replace("\\", "\\\\").replace("\n", "\\n").replace('"', '\\"') + '\\n"))'

        if newline != "":
          outf.write(newline + "\n")
        outf.write("    ]\n")

  finally:
    if outfilename is not None:
      outf.close()

pass
